package org.hfy.controller;

import org.hfy.common.ApiResponse;
import org.hfy.dto.LoginRequestDTO;
import org.hfy.dto.LoginResponseDTO;
import org.hfy.exception.BusinessException;
import org.hfy.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 身份验证Controller
 * 
 * @author hfy
 * @version 1.0
 */
@RestController
@RequestMapping("/auth")
@Validated
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 登录
     * 
     * @param loginRequest 登录请求
     * @return 登录响应
     */
    @PostMapping("/login")
    public ApiResponse<LoginResponseDTO> login(@Valid @RequestBody LoginRequestDTO loginRequest) {
        try {
            LoginResponseDTO loginResponse = authService.login(loginRequest);
            return ApiResponse.success(loginResponse);
        } catch (BusinessException e) {
            return ApiResponse.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("登录失败：" + e.getMessage());
        }
    }
    
    /**
     * 验证token
     * 
     * @param token 令牌
     * @return 是否有效
     */
    @GetMapping("/validate")
    public ApiResponse<Boolean> validateToken(@RequestParam String token) {
        try {
            boolean valid = authService.validateToken(token);
            return ApiResponse.success(valid);
        } catch (Exception e) {
            return ApiResponse.error("验证失败：" + e.getMessage());
        }
    }
} 